/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bo;

import dao.DesignLayerDAO;
import dao.DesignationDAO;
import data.Desiglayer;
import data.Designation;
import java.awt.TextField;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ComboBoxModel;
import javax.swing.JTextField;
import javax.swing.table.TableModel;
import org.hibernate.HibernateException;
import view.ManageDesignation;
import view.ManageDesignationLayer;

/**
 *
 * @author Hùng
 */
public class DesignLayerBO {

    private String Success;

    public DesignLayerBO() {
    }

    public DesignLayerBO(String Success) {
        this.Success = Success;
    }

    public String getSuccess() {
        return Success;
    }

    public void setSuccess(String Success) {
        this.Success = Success;
    }

    public List<Desiglayer> showAllLayerOfDesign(String Design) {
        List<Desiglayer> lDlayer = new DesignLayerDAO().getAllDesignLayer(Design);
        return lDlayer;
    }

    public void createDesignLayer(Object cbDesign, JTextField txtlayer, String tmpWeightAge) {
        String tmplayer = txtlayer.getText();
        if (cbDesign != null && !tmplayer.equals("") && !tmpWeightAge.equals("")) {
            String Design = (String) cbDesign;
            Designation tmpD = new DesignationDAO().getDesignationbyDesignation(Design);
            int layer = Integer.parseInt(tmplayer);
            Desiglayer tmpDLayer = new DesignLayerDAO().getDesignLayerByIdLayer(layer);
            if (tmpDLayer != null) {
                txtlayer.setText("");
                throw new Error("Layer is existed ");
            }
            int WeightAge = Integer.parseInt(tmpWeightAge);
            Desiglayer tmpDlayer = new Desiglayer(
                    layer,
                    tmpD,
                    WeightAge,
                    null);
            boolean flag = new DesignLayerDAO().executeInsertHQLQuery(tmpDlayer);
            if (flag == true) {
                Success = "Create Layer of Designation SuccessFully";
            } else {
                throw new Error("Create Layer of Designation Fail");
            }
        } else {
            throw new Error("Please,check your information");
        }

    }

    public void removeDesignLayer(int[] selectedRows, TableModel model) {
        if (model.getRowCount() > 0) {
            if (selectedRows.length > 0) {
                List<Desiglayer> lDLayer = new ArrayList<Desiglayer>();
                try {

                    for (int i = 0; i < selectedRows.length; i++) {
                        int tmplayer = (int) model.getValueAt(selectedRows[i], 1);
                        Desiglayer tmpDLayer = new DesignLayerDAO().getDesignLayerByIdLayer(tmplayer);
                        if (tmpDLayer.getDeptposweightages().size() > 0) {
                            throw new Error("Delete Layer Fail.Please,Delete List Deptposweightage's details of Designation");
                        }
                        lDLayer.add(tmpDLayer);
                    }
                } catch (HibernateException he) {
                    throw new Error(he.getMessage());

                }
                for (Desiglayer a : lDLayer) {
                    new DesignLayerDAO().executeDeleteHQLQuery(a);
                }
                Success = "Delete Layer of designation Successfully";
            } else {
                throw new Error("Please,select rows on the table to delete");
            }
        } else {
            throw new Error("Please,select Designation  to delete");
        }
    }

    public void showADesignLayer(int[] rows, TableModel model, String idLayer, String weightage) {
        if (model.getRowCount() > 0) {
            if (rows.length == 1) {
                int idDLayer = (int) model.getValueAt(rows[0], 1);
                Desiglayer tmpDLayer = new DesignLayerDAO().getDesignLayerByIdLayer(idDLayer);
                ManageDesignationLayer.setsDLayer(tmpDLayer);
            } else {
                throw new Error("Please,select only Row on the table to edit");
            }
        } else {
            throw new Error("Please,select Designation  to edit");
        }

    }

    public void EditDesignLayer(String tmpIdlayer, String tmpWeightage) {
        try {
            int idDlayer = Integer.parseInt(tmpIdlayer);
            int Weightage = Integer.parseInt(tmpWeightage);
            Desiglayer tmpD = new DesignLayerDAO().getDesignLayerByIdLayer(idDlayer);
            tmpD.setWeightage(Weightage);
            boolean flag = new DesignLayerDAO().executeUpdateHQLQuery(tmpD);
            if(flag == true){
                Success = "Edit Layer of designation Successfully";
            }else{
                throw new Error("Edit Layer of designation Fail");
            }
        } catch (HibernateException he) {
            throw new Error(he.getMessage());
        }

    }

	public ComboBoxModel getComBoBox() {
		return new DesignLayerDAO().getComBoBox();
	}
}
